﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace DrivingCRM.Views
{
    public partial class MasterPage : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            /// si l'utilisateur est authentifié et que son type en session n'est pas null
            if (HttpContext.Current.User.Identity.IsAuthenticated && Session["User"] != null)
            {
                if ((Session["User"] as List<string>)[0] == "Salesman")
                {
                    this.MV_DynamicMenu.ActiveViewIndex = 1;
                    this.B_LogOut.Style.Add("display", "visible");
                    this.L_User.Style.Add("display", "visible");
                    this.L_User.Text = (Session["User"] as List<string>)[1];
                }
                else if ((Session["User"] as List<string>)[0] == "Administrator")
                {
                    this.MV_DynamicMenu.ActiveViewIndex = 2;
                    this.B_LogOut.Style.Add("display", "visible");
                    this.L_User.Style.Add("display", "visible");
                    this.L_User.Text = (Session["User"] as List<string>)[1];
                }
                else if ((Session["User"] as List<string>)[0] == "Stakeholder")
                {
                    this.MV_DynamicMenu.ActiveViewIndex = 3;
                    this.B_LogOut.Style.Add("display", "visible");
                    this.L_User.Style.Add("display", "visible");
                    this.L_User.Text = (Session["User"] as List<string>)[1];
                }
                else if ((Session["User"] as List<string>)[0] == "Client")
                {
                    this.MV_DynamicMenu.ActiveViewIndex = 4;
                    this.B_LogOut.Style.Add("display", "visible");
                    this.L_User.Style.Add("display", "visible");
                    this.L_User.Text = (Session["User"] as List<string>)[1];
                }
                else
                {
                    FormsAuthentication.SignOut();
                    Session.Clear();
                    this.B_LogOut.Style.Add("display", "none");
                    this.L_User.Style.Add("display", "none");
                    FormsAuthentication.RedirectToLoginPage();
                }
            }
            else
            {
                /// active la vue anonyme
                this.MV_DynamicMenu.ActiveViewIndex = 0;
                this.B_LogOut.Style.Add("display", "none");
                this.L_User.Style.Add("display", "none");
            }
        }

        /// <summary>
        /// Méthode déclenchée au clic sur le bouton de déconnexion
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void B_LogOut_Click(object sender, EventArgs e)
        {
            FormsAuthentication.SignOut();
            Session.Clear();
            FormsAuthentication.RedirectToLoginPage();
        }
    }
}